import { Module, Store } from "vuex";
import { BaiyeService, Activity } from "@/api/baiye";
import Code from "@/consts/common";
import { ApiReturn } from "@/utils/request";
const createModule = <S, R>(store: Module<S, R>) => store as Store<S>;
//https://zhuanlan.zhihu.com/p/143884250
export interface ActiveState {
  total: number;
  loading: boolean;
  actives: Activity[];
  page: number;
  size: number;
}

let state: ActiveState = {
  total: 0,
  loading: false,
  actives: [],
  page: 1,
  size: 15,
};
// 定义一个 module
export default createModule({
  namespaced: true,
  state: state,
  actions: {
    listActive({ commit }, params: any) {
      commit("changeLoading");
      BaiyeService.listActivity(params).then((res) => {
        if (res.code == Code.CODE_SUCCESS) {
          commit("listActive", res.data);
        }
        commit("changeLoading");
      });
    },
    pageChange({ commit, dispatch }, data: any) {
      commit("pageChange", data.page);
      dispatch("listActive", data);
    },
    pageSizeChange({ commit, dispatch }, data: any) {
      commit("pageSizeChange", data.size);
      commit("pageChange", 1);
      dispatch("listActive", data);
    },
    save(_, data: any) {
      return BaiyeService.saveBaiyeActive(data)
    },
    deleteActive(_, data: number[]): ApiReturn<any> {
      return BaiyeService.deleteActive(data)
    }
  },
  mutations: {
    listActive(state, data) {
      state.total = data.total;
      state.actives = data.data;
    },
    pageChange(state, page) {
      state.page = page;
    },
    changeLoading(state) {
      state.loading = !state.loading;
    },
    pageSizeChange(state, size) {
      state.size = size;
    },
  },
});
